/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.chanceit.ssss.activity;

import org.gl.customview.ViewPager;
import org.gl.customview.viewpager.PagerSlidingTabStrip;
import org.gl.utils.lg;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import com.chanceit.ssss.adapters.SectionsPagerAdapter;
import com.chanceit.ssss.common.IntentExtraDataKey;
import com.chanceit.ssss.fragments.CarerListFragment.OnListFragmentSelectedListener;

public class HomeActivity extends BaseFragmentActivity implements
		OnListFragmentSelectedListener {
	SectionsPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_articles);
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager(), this);

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		int selectIndex = getIntent().getIntExtra(IntentExtraDataKey.HOME_SELECT_PAGE, 0);
		if (selectIndex>mSectionsPagerAdapter.getCount()) {
			selectIndex = 0;
			lg.e("HomeActivity","selectIndex>mSectionsPagerAdapter.getCount()");
		}
		mViewPager.setCurrentItem(selectIndex);
		final int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
						.getDisplayMetrics());
		mViewPager.setPageMargin(pageMargin);
		PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		tabs.setViewPager(mViewPager);
		tabs.setIndicatorColor(0xFF3F9FE0);
		tabs.setVisibility(View.GONE);
		
		// Check whether the activity is using the layout version with
		// the fragment_container FrameLayout. If so, we must add the first
		// fragment
		if (findViewById(R.id.fragment_container) != null) {

			// However, if we're being restored from a previous state,
			// then we don't need to do anything and should return or else
			// we could end up with overlapping fragments.
			if (savedInstanceState != null) {
				return;
			}
			// // Create an instance of ExampleFragment
			// HeadlinesFragment firstFragment = new HeadlinesFragment();
			//
			// // In case this activity was started with special instructions
			// from
			// // an Intent,
			// // pass the Intent's extras to the fragment as arguments
			// firstFragment.setArguments(getIntent().getExtras());
			//
			// // Add the fragment to the 'fragment_container' FrameLayout
			// getSupportFragmentManager().beginTransaction()
			// .add(R.id.fragment_container, firstFragment).commit();
			// getAlluser();
		}
	}
	public void onArticleSelected(int position) {
		// The user selected the headline of an article from the
		// HeadlinesFragment

		// Capture the article fragment from the activity layout
		// ArticleFragment articleFrag = (ArticleFragment)
		// getSupportFragmentManager()
		// .findFragmentById(R.id.article_fragment);
		//
		// if (articleFrag != null) {
		// // If article frag is available, we're in two-pane layout...
		//
		// // Call a method in the ArticleFragment to update its content
		// articleFrag.updateArticleView(position);
		//
		// } else {
		// // If the frag is not available, we're in the one-pane layout and
		// // must swap frags...
		//
		// // Create fragment and give it an argument for the selected article
		// ArticleFragment newFragment = new ArticleFragment();
		// Bundle args = new Bundle();
		// args.putInt(ArticleFragment.ARG_POSITION, position);
		// newFragment.setArguments(args);
		// FragmentTransaction transaction = getSupportFragmentManager()
		// .beginTransaction();
		//
		// // Replace whatever is in the fragment_container view with this
		// // fragment,
		// // and add the transaction to the back stack so the user can
		// // navigate back
		// transaction.replace(R.id.fragment_container, newFragment);
		// transaction.addToBackStack(null);
		//
		// // Commit the transaction
		// transaction.commit();
		// }
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult( requestCode,  resultCode,  data);
		switch (requestCode) {
		case IntentExtraDataKey.ACTIVITY_QUESTCODE_UPDATECARERINFO:
			if (resultCode==200) {
				mSectionsPagerAdapter.sendMessage(2);
//				mSectionsPagerAdapter.sendMessage(1);
			}
			
			break;

		default:
			mSectionsPagerAdapter.sendMessage(2);
			mSectionsPagerAdapter.sendMessage(1);
			break;
		}
	}
}